//
//  LeftMenusView.m
//  SUZHOUBANK
//
//  Created by saberLily on 16/4/11.
//  Copyright © 2016年 RuiFeng-WGCC. All rights reserved.
//

#import "LeftMenusView.h"
#import "DefineHeader.h"
#import "ImageCellView.h"
@interface LeftMenusView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
{
    NSArray *_dataSource;
}

@property (nonatomic,strong) UICollectionView *collectionview; /**< 左侧集合视图 */
@property (nonatomic,strong) UICollectionViewFlowLayout *flowLayout; /**< 流式布局 */

@property (nonatomic,strong) UIView *triangleView; /**< 可移动三角形 */

@end
@implementation LeftMenusView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initializeDataSource];
        [self initializeUserInterface];
    }
    return self;
}
#pragma mark - initializeDataSource 初始化数据源
-(void)initializeDataSource{
    _dataSource = @[@"金融资讯",@"银行业务",@"投资理财",@"市民卡",@"对公产品",@"网点地图",@"我的银行"];
}

#pragma mark - initializeUserInterface 初始化用户界面
-(void)initializeUserInterface{
    [self addSubview:self.collectionview];
    [self.collectionview addSubview:self.triangleView];
}
#pragma mark *** UICollectionViewDetaSource ***

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _dataSource.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ImageCellView *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kUIMainViewCollectionViewCell forIndexPath:indexPath];
    
    cell.cellImage.image = [UIImage imageNamed:_dataSource[indexPath.row]];

    return cell;
    
}

#pragma mark *** UICollectionViewDelegate ***

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    [UIView animateWithDuration:SelectedTriangleAnimationTime animations:^{
        self.triangleView.frame = AdaptationFrame(100,30+indexPath.row*96 , 20, 20);
    }];
    
    if (_delegate && [_delegate respondsToSelector:@selector(LeftMenusView:didSelectedCellAtIndexPathRow:)]) {
        [_delegate LeftMenusView:self didSelectedCellAtIndexPathRow:indexPath.row];
    }
    
    
}
#pragma mark *** getters ***

-(UICollectionView *)collectionview{
    if (!_collectionview) {
        _collectionview = [[UICollectionView alloc] initWithFrame:AdaptationFrame(0, 0, 110, 668) collectionViewLayout:self.flowLayout];
        _collectionview.dataSource = self;
        _collectionview.delegate = self;
        _collectionview.bounces = NO;
        
        _collectionview.backgroundColor = [UIColor clearColor];
        [_collectionview registerClass:[ImageCellView class] forCellWithReuseIdentifier:kUIMainViewCollectionViewCell];
        
    }
    return _collectionview;
}

-(UICollectionViewFlowLayout *)flowLayout{
    if (!_flowLayout) {
        _flowLayout = [[UICollectionViewFlowLayout alloc] init];
        _flowLayout.itemSize = AdaptationSize(94, 94);
        _flowLayout.minimumLineSpacing = 1*AdaptationWidth();
        _flowLayout.minimumInteritemSpacing = 5*AdaptationWidth();
        
    }
    return _flowLayout;
}
-(UIView *)triangleView{
    if (!_triangleView) {
        _triangleView =[[UIView alloc] initWithFrame:AdaptationFrame(100,30 , 20, 20)];
        _triangleView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"选中指示三角"]];
        
    }
    return _triangleView;
}
@end
